//
//  YDYTopicCell.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/8/5.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYTopicCell.h"
#import <UIImageView+WebCache.h>
#import "YDYComment.h"
#import "YDYUser.h"
#import "YDYTopicVideoView.h"
#import "YDYTopicVoiceView.h"
#import "YDYTopicPictureView.h"
@interface YDYTopicCell()

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *creatAtLabel;
@property (weak, nonatomic) IBOutlet UILabel *text_Label;
//工具栏
@property (weak, nonatomic) IBOutlet UIButton *dingBtn;
@property (weak, nonatomic) IBOutlet UIButton *caiBtn;
@property (weak, nonatomic) IBOutlet UIButton *repostBtn;
@property (weak, nonatomic) IBOutlet UIButton *commentBtn;
//最热评论
@property (weak, nonatomic) IBOutlet UILabel *topCmtContentLabel;
//最热评论整体
@property (weak, nonatomic) IBOutlet UIView *topCmtView;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
//中间用到的view
/** picture */
@property (nonatomic, weak) YDYTopicPictureView *pictureView;
/** video */
@property (nonatomic, weak) YDYTopicVideoView *videoView;
/** voice */
@property (nonatomic, weak) YDYTopicVoiceView *voiceView;

@end
@implementation YDYTopicCell
-(void)setFrame:(CGRect)frame{
    //cell间间距
    frame.size.height -= YDYCellMargin;
    //设置左右间距
    frame.origin.x += 3;
    frame.size.width -= 6;
    //最上面的cell距离顶部的距离
    frame.origin.y +=10 ;
    [super setFrame:frame];
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
    
    
    
}


-(void)setTopic:(YDYTopic *)topic{
    _topic = topic;
    UIImage *defaultImage = [UIImage imageNamed:@"defaultUserIcon"];
    [self.profileImageView ydy_setCircleHeader:self.topic.profile_image PlaceHolderImage:defaultImage];
//    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:self.topic.profile_image] placeholderImage:defaultImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        if (!image)return ;
//        
//        self.profileImageView.image = [image ydy_circleImage];
//    }];
    
    self.nameLabel.text = topic.name;
    self.text_Label.text = topic.text;
    self.creatAtLabel.text = topic.created_at;
    
    //设置工具栏上面的按钮显示
    [self setupBtn:self.dingBtn number:topic.ding placeholder:@"顶"];
    [self setupBtn:self.caiBtn number:topic.cai placeholder:@"踩"];
    [self setupBtn:self.commentBtn number:topic.comment placeholder:@"评论"];
    [self setupBtn:self.repostBtn number:topic.repost placeholder:@"转发"];
    //设置最热评论控件
    if (topic.top_cmt) {
        self.topCmtView.hidden = NO;
        //取出评论
        NSString *name = topic.top_cmt.user.username;
        NSString *text = topic.top_cmt.content;
        //判断是不是语音评论
        if (topic.top_cmt.voiceuri.length) {
            text = @"语音评论";
        }
        self.topCmtContentLabel.text = [NSString stringWithFormat:@"%@:%@",name,text];
    }else{ //没有最热评论
        self.topCmtView.hidden = YES;
    }
    //设置中间内容
    
    
    switch (topic.type) {
        case YDYTopicTypePicture:
            
            self.pictureView.hidden = NO;
            self.voiceView.hidden = YES;
            self.videoView.hidden = YES;
            self.pictureView.frame = topic.contentF;
            self.pictureView.topic = topic;
            break;
        
        case YDYTopicTypeVideo:
            self.pictureView.hidden = YES;
            self.voiceView.hidden = YES;
            self.videoView.hidden = NO;
            self.videoView.frame = topic.contentF;
            self.videoView.topic = topic;
        
            break;
        case YDYTopicTypeVoice:
            self.pictureView.hidden = YES;
            self.voiceView.hidden = NO;
            self.videoView.hidden = YES;
            self.voiceView.frame = topic.contentF;
            self.videoView.topic = topic;
           
            break;
        case YDYTopicTypeWord:
            self.pictureView.hidden = YES;
            self.voiceView.hidden = YES;
            self.videoView.hidden = YES;
            break;
        default:
            YDYLog(@"这是什么类？");
            break;
    }
}


-(void)setupBtn:(UIButton *)btn number:(NSInteger)number placeholder:(NSString *)place{
    if (number >= 10000) {
        [btn setTitle:[NSString stringWithFormat:@"%.1f万",number / 10000.0] forState:UIControlStateNormal];
        
    }else if (number >0){
        [btn setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else{
        [btn setTitle:[NSString stringWithFormat:@"%@",place] forState:UIControlStateNormal];
    }
}
- (IBAction)moreBtn:(id)sender {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更多" message:@"更多内容" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        YDYLog(@"点击了收藏");
    }];
    
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        YDYLog(@"点击了举报");
    }];
    UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        YDYLog(@"点击了取消");
    }];
    [alertVC addAction:act1];
    [alertVC addAction:act2];
    [alertVC addAction:act3];
    
    [self.window.rootViewController presentViewController:alertVC animated:YES completion:^{
        
    }];
    
}

#pragma mark -
#pragma mark -懒加载
-(YDYTopicPictureView *)pictureView{
    if (!_pictureView ) {
        YDYTopicPictureView *pictureview = [YDYTopicPictureView ydy_viewfromXib];
        [self.contentView addSubview:pictureview];
        _pictureView = pictureview;
        return _pictureView;
    }
    
    return _pictureView;
}

-(YDYTopicVoiceView *)voiceView{
    if (!_voiceView) {
        YDYTopicVoiceView *voiceView = [YDYTopicVoiceView ydy_viewfromXib];
        [self.contentView addSubview:voiceView];
        _voiceView = voiceView;
    }
  
    return _voiceView;
}
-(YDYTopicVideoView *)videoView{
    if (!_videoView) {
        YDYTopicVideoView *videoView = [YDYTopicVideoView ydy_viewfromXib];
        [self.contentView addSubview:videoView];
        _videoView = videoView;
    }
    return _videoView;
}
@end
